<?php

namespace app\index\controller;

use think\Controller;

class Market extends Base {
	function index() {
		// $key = 'KI6TXIA5AEBSWRRW9IJIMIFGVWMQ9QIRKIAYCP7B';
		// $mf = new \Mifengcha ( $key );
		// $path = '/api/v3/tickers';
		// $result = $mf->curl_post ( $path, array ('market' => 'binance' ) );
		// $result = json_decode ( $result, true );
		// unset ( $data );
		// if (count ( $result ) > 0) {
		// foreach ( $result as $k => $v ) {
		// $instId = explode ( '_', $v ['m'] );
		// $result [$k] ['instId0'] = $instId [0];
		// $result [$k] ['instId1'] = $instId [1];
		// $result [$k] ['instId2'] = $instId [2];
		// }
		// }
		$key = '1af61d2c-0949-491a-91c1-123d83c57d19';
		$secret = 'F421319CDF46AE1705FCC4B09D3CCCD9';
		date_default_timezone_set ( "UTC" );
		$okx = new \Okx ( $key, $secret );
		$method = 'GET';
		$path = '/api/v5/market/tickers';
		$times = str_replace ( '+00:00', 'Z', gmdate ( 'c' ) );
		$pwd = 'Uu888888..';
		
		$data ['instType'] = 'SWAP';
		
		$str = $times . $method . $path . json_encode ( $data );
		$sign = $okx->sign ( $str );
		$headers = $okx->header ( $key, $sign, $times, $pwd );
		// $result = $okx->curl_post ( 'https://www.okx.com/api/v5/market/tickers?instType=SWAP', '', $headers );
		$result = file_get_contents ( 'okx.json' );
		$result = json_decode ( $result, true );
		unset ( $data );
		if ($result ['code'] == 0) {
			$data = $result ['data'];
			$coin = array ('BTC-USDT-SWAP','ETH-USDT-SWAP','DOGE-USDT-SWAP','EOS-USDT-SWAP','LTC-USDT-SWAP','BCH-USDT-SWAP','XRP-USDT-SWAP','ETC-USDT-SWAP','BSV-USDT-SWAP','XMR-USDT-SWAP','DOT-USDT-SWAP','ADA-USDT-SWAP','FIL-USDT-SWAP','TRX-USDT-SWAP','UNI-USDT-SWAP','LINK-USDT-SWAP','SOL-USDT-SWAP','AAVE-USDT-SWAP','HT-USDT-SWAP' );
			$i = 0;
			foreach ( $data as $k => $v ) {
				if (in_array ( $v ['instId'], $coin )) {
					$instId = explode ( '-', $v ['instId'] );
					$dt [$i] = $v;
					$dt [$i] ['instId0'] = $instId [0];
					$dt [$i] ['instId1'] = $instId [1];
					$dt [$i] ['instId2'] = $instId [2];
					$i ++;
				}
			}
		}
		$this->assign ( 'okx', $dt );
		echo $this->fetch ()->getContent ();
	}
}
